package com.dreamer.domain.mall.transfer;

import com.dreamer.util.PreciseComputeUtil;

import java.util.*;
import java.util.stream.Collectors;

public class VoucherVo {

    private String name;

    private Double amount;

    private String info = "";

    public VoucherVo() {

    }

    public static List<VoucherVo> countFromTransferVos(List<TransferVo> transferVos, Integer type) {
        Map<String, VoucherVo> resultMap = new HashMap<>();
        transferVos.forEach(transferVo -> {

            transferVo.getItemVOs().forEach(itemVO -> {
                String bkey;
                bkey = transferVo.getAdd() == 0 ? transferVo.getFromUser() : transferVo.getToUser();
                //直接利润
                Double v1 = itemVO.getV(1) * itemVO.getQuantity();
                //分享利润
                Double v2 = itemVO.getV(2) * itemVO.getQuantity();
                if (transferVo.getAdd().equals(0)) {
                    v1 = -v1;
                    v2 = -v2;
                }
                String addTem = transferVo.getAdd() == 1 ? "进货" : "退货";
                String item = transferVo.getAddLevel() == 1 ? itemVO.getName()+"X"+itemVO.getQuantity() + "进货" : transferVo.getItem() + "进货";
                //进钱 谁要给我钱
                if (type == 1) {
                    //供货人 出钱
                    String pkey = transferVo.getAdd() == 1 ? transferVo.getFromUser() : transferVo.getToUser();
                    if (transferVo.getAddLevel() == 1) {
                        //供货人要给我一级钱
                        setVoToMap(bkey, pkey, addTem, v1, item, resultMap);
                    } else {
                        //供货人要给我二级钱
                        setVoToMap(bkey, pkey, addTem, v2, item, resultMap);
                    }
                }
                //出钱
                if (type == 0) {
                    //给直接转货人钱
                    setVoToMap(bkey, bkey, addTem, v1, item, resultMap);
                    //还要给二级供货人钱
                    if (transferVo.getAddLevel() == 2) {
                        //给购买人的上级二级钱
                        setVoToMap(bkey, transferVo.getParentUser(), addTem, v2, item, resultMap);
                    }
                }
            });
        });
        return resultMap.values().stream().collect(Collectors.toList());
    }


    private static void setVoToMap(String buyAgent, String key, String addTem, Double d, String item, Map<String, VoucherVo> resultMap) {
        //给钱出去
        VoucherVo voucherVo;
        if (resultMap.containsKey(key)) {
            voucherVo = resultMap.get(key);
            Double temD = PreciseComputeUtil.add(voucherVo.getAmount(), d);
            voucherVo.setAmount(temD);
            voucherVo.setInfo(voucherVo.getInfo() + buyAgent + addTem + " [" + item +"折让"+d+ "元 ]" + "\n" +
                    "\n");
        } else {
            voucherVo = new VoucherVo();
            voucherVo.setAmount(d);
            voucherVo.setName(key);
            voucherVo.setInfo(buyAgent + addTem + " [" + item +"折让"+d+ "元 ]" + "\n" +
                    "\n");
        }
        resultMap.put(key, voucherVo);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }
}
